package easyjava.hibernate.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import easyjava.hibernate.bean.Colleage;

public class ColleageDao {
     private Session session;
     public ColleageDao(Session session){
    	 this.session=session;
     }
     public List<Colleage> list(){
    	 Query<Colleage> query =session.createQuery("from Colleage",Colleage.class);
    	 return query.getResultList();
     }
 	public void insert(Colleage colleage) {
		Transaction tx = session.beginTransaction();
		try {

			session.save(colleage);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}
	}
	public void update(Colleage colleage){
		Transaction tx = session.beginTransaction();
		try {

			session.update(colleage);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}
		
	}
	public void delete(Colleage colleage){
		Transaction tx = session.beginTransaction();
		try {
			
			session.delete(colleage);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}
	}	
}
